package com.example.bookstore.web;

import com.example.bookstore.pojo.Book;
import com.example.bookstore.pojo.Page;
import com.example.bookstore.service.BookService;
import com.example.bookstore.service.impl.BookServiceImpl;
import com.example.bookstore.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class BookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
        bookService.addBook(book);
//        req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
        //请求转发会出现按F5多次请求的bug
//        System.out.println(req.getContextPath());
        resp.sendRedirect(req.getContextPath() +  "/manager/bookServlet?action=list");
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        bookService.deleteBookById(id);
//        System.out.println(req.getContextPath());
        resp.sendRedirect(req.getContextPath() +  "/manager/bookServlet?action=list");

    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
        bookService.updateBook(book);
        resp.sendRedirect(req.getContextPath() +  "/manager/bookServlet?action=list");
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        List<Book> books = bookService.queryBooks();
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);

    }

    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Book book = bookService.queryBookById(id);
        req.setAttribute("book",book);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
//        Cookie[] cookies = req.getCookies();
//        for (Cookie cookie : cookies) {
//            if("username".equals(cookie.getName())){
                int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
                int pageSize = WebUtils.parseInt(req.getParameter("pageSize"),Page.PAGE_SIZE);
                Page<Book> page = bookService.page(pageNo,pageSize);
                req.setAttribute("page",page);
                req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
//                break;
//            }
//        }
//        req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);


    }
}

